<?php
class Model_Core_Session_Abstract
{
    protected $_session = null;

    protected $_nameSpace = null;

    protected static $_underscoreCache = array();

    public function __construct()
    {
        $this->_session = new Zend_Session_Namespace($this->_nameSpace);
    }

    public function addError($text)
    {
        $data = $this->getData('message');
        if(!isset($data)) {
            $data = array();
        }

        $data['error'][] = $text;
        $this->setData('message',$data);

        return $this;
    }

    public function addNotice($text)
    {
        $data = $this->getData('message');
        if(!isset($data)) {
            $data = array();
        }

        $data['notice'][] = $text;
        $this->setData('message',$data);

        return $this;
    }

    public function addSuccess($text)
    {
        $data = $this->getData('message');
        if(!isset($data)) {
            $data = array();
        }

        $data['success'][] = $text;
        $this->setData('message',$data);

        return $this;
    }

    public function setData($key, $value=null)
    {
        if(!isset($this->_session->data)) {
            $this->_session->data = new Object();
        }
        $this->_session->data->setData($key,$value);
        return $this;
    }

    public function unsetData($key=null)
    {
        if(!isset($this->_session->data)) {
            $this->_session->data = new Object();
        }
        $this->_session->data->unsetData($key);
        return $this;
    }

    public function getData($key='', $index=null)
    {
        if(!isset($this->_session->data)) {
            $this->_session->data = new Object();
        }
        return $this->_session->data->getData($key,$index);
    }

    public function hasData($key='')
    {
        if(!isset($this->_session->data)) {
            $this->_session->data = new Object();
        }
        return $this->_session->data->hasData($key);
    }

    public function __call($method, $args)
    {
        switch (substr($method, 0, 3)) {
            case 'get' :
                $key = $this->_underscore(substr($method,3));
                $data = $this->getData($key, isset($args[0]) ? $args[0] : null);
                return $data;

            case 'set' :
                $key = $this->_underscore(substr($method,3));
                $result = $this->setData($key, isset($args[0]) ? $args[0] : null);
                return $result;

            case 'uns' :
                $key = $this->_underscore(substr($method,3));
                $result = $this->unsetData($key);
                return $result;

            case 'has' :
                $key = $this->_underscore(substr($method,3));
                return isset($this->_data[$key]);
        }
        throw new Zend_Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");
    }

    protected function _underscore($name)
    {
        if (isset(self::$_underscoreCache[$name])) {
            return self::$_underscoreCache[$name];
        }
        $result = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name));
        self::$_underscoreCache[$name] = $result;
        return $result;
    }
}